package com.ease.framework.gef4p.editparts {
   import com.ease.common.models.IModel;
   import com.ease.framework.gef.EditPart;
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.commands.CreateNodeCommand;
   import com.ease.framework.gef4p.commands.MoveResizeVertexCommand;
   import com.ease.framework.gef4p.commands.RemoveNodeCommand;
   import com.ease.framework.gef4p.figures.ILabelFigureBox;
   import com.ease.framework.gef4p.figures.INodeFigure;
   import com.ease.framework.gef4p.figures.edge.IEdgeFigure;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.gef4p.model.Pool;
   import com.ease.framework.gef4p.model.Vertex;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;

   import flash.events.ContextMenuEvent;
   import flash.geom.Point;
   import flash.ui.ContextMenuItem;

   import mx.collections.ArrayCollection;
   import mx.core.UIComponent;

   public class NodeEditPart extends EditPart {

      public var inTransitions:ArrayCollection = new ArrayCollection();

      public var outTransitions:ArrayCollection = new ArrayCollection();

      public function NodeEditPart(obj:IModel) {
         super(obj);
      }

      override public function doRefreashFigure():void {
         figure.x = Node(model).x;
         figure.y = Node(model).y;
         if (Node(model).width != 0) {
            figure.width = Node(model).width;
            figure.height = Node(model).height;
         }
         INodeFigure(figure).onMoving();
         ILabelFigureBox(figure).getLabelFigure().text = Node(model).name;
         UIComponent(figure).invalidateDisplayList();
      }

      override public function renderFigure():void {
      }

      public function addInEdge(inTran:EdgeEditPart):void {
         inTransitions.addItem(inTran);
         Node(model).addInEdge(inTran.model as Edge);
         INodeFigure(figure).addInEdge(inTran.figure as IEdgeFigure);
      }

      public function removeInEdge(inTran:EdgeEditPart):void {
         if (inTransitions.contains(inTran)) {
            inTransitions.removeItemAt(inTransitions.getItemIndex(inTran));
            Node(model).removeInEdge(inTran.model as Edge);
         }
         INodeFigure(figure).removeInEdge(inTran.figure as IEdgeFigure);
      }

      public function addOutEdge(outTran:EdgeEditPart):void {
         outTransitions.addItem(outTran);
         Node(model).addOutEdge(outTran.model as Edge);
         INodeFigure(figure).addOutEdge(outTran.figure as IEdgeFigure);
      }

      public function removeOutEdge(outTran:EdgeEditPart):void {
         if (outTransitions.contains(outTran)) {
            outTransitions.removeItemAt(outTransitions.getItemIndex(outTran));
            Node(model).removeOutEdge(outTran.model as Edge);
         }
         INodeFigure(figure).removeOutEdge(outTran.figure as IEdgeFigure);
      }

      override public function createNewCommand(pool:IModel, parent:IModel = null, point:Point = null):ICommand {
         var node:Node = model as Node;
         if (point != null) {
            node.x = point.x - node.width / 2;
            node.y = point.y - node.height / 2;
         }

         return new CreateNodeCommand(pool as Pool, node);
      }

      override public function createEditCommand(newModel:IModel):ICommand {
         return null;
      }

      override public function createRemoveCommand():ICommand {
         return new RemoveNodeCommand(model as Node);
      }

      override public function createMoveResizeCommand(gap_x:Number, gap_y:Number, gap_width:Number, gap_height:Number):ICommand {
         return new MoveResizeVertexCommand(model as Vertex, gap_x, gap_y, gap_width, gap_height);
      }
   }
}